package site.jlopen.secret.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.lang.TypeReference;
import site.jlopen.secret.client.SecretClientHttp;
import site.jlopen.secret.client.SecretReqUrl;
import site.jlopen.secret.common.SecretClientResponse;
import site.jlopen.secret.common.SecretReqBaseForm;
import site.jlopen.secret.config.SecretClientProperties;
import site.jlopen.secret.model.data.SecretDatasourceCreateReq;
import site.jlopen.secret.model.data.SecretDatasourceInfoResp;
import site.jlopen.secret.service.intf.SecretDatasourceManage;

public class SecretDatasourceImpl implements SecretDatasourceManage {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private final SecretClientProperties clientProperties;
	
	public SecretDatasourceImpl(SecretClientProperties clientProperties) {
		this.clientProperties = clientProperties;
	}

	@Override
	public SecretClientResponse<String> datasourceCreate(SecretDatasourceCreateReq form) {
		logger.info("绑定节点数据源请求：{}", form);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DS_CREATE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> datasourceUpdate(SecretDatasourceCreateReq form) {
		logger.info("修改节点数据源请求：{}", form);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DS_UPDATE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> datasourceRemove(String datasourceCode) {
		logger.info("移除数据源请求：datasourceCode：{}", datasourceCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		SecretReqBaseForm<String> form = new SecretReqBaseForm<String>()
		.add("datasourceCode", datasourceCode)
		.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DS_REMOVE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> datasourceBind(String datasourceCode, String nodeCode) {
		logger.info("绑定节点数据源请求：datasourceCode：{}、nodeCode：{}", datasourceCode, nodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		SecretReqBaseForm<String> form = new SecretReqBaseForm<String>().add("nodeCode", nodeCode)
		.add("datasourceCode", datasourceCode)
		.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DS_BIND_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> datasourceUnbind(String datasourceCode, String nodeCode) {
		logger.info("解绑节点数据源请求：datasourceCode：{}、nodeCode：{}", datasourceCode, nodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		SecretReqBaseForm<Object> form = new SecretReqBaseForm<Object>().add("datasourceCode", datasourceCode)
				.add("nodeCode", nodeCode)
		.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DS_UNBIND_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> datasourceStatus(String datasourceCode, String nodeCode) {
		logger.info("刷新节点数据源请求：datasourceCode：{}、nodeCode：{}", datasourceCode, nodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		SecretReqBaseForm<Object> form = new SecretReqBaseForm<Object>().add("datasourceCode", datasourceCode)
				.add("nodeCode", nodeCode)
		.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DS_STATUS_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<SecretDatasourceInfoResp> datasourceInfo(String datasourceId) {
		logger.info("查询节点数据源请求：{}", datasourceId);
		SecretClientHttp<SecretDatasourceInfoResp> clientRequest = new SecretClientHttp<SecretDatasourceInfoResp>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>()
				.add("datasourceId", datasourceId)
				.appId(clientProperties.getAppId());
		SecretClientResponse<SecretDatasourceInfoResp>  res = clientRequest.execute(SecretReqUrl.DS_INFO_URL, form, new TypeReference<SecretClientResponse<SecretDatasourceInfoResp>>() {});
		return res;
	}

}
